﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Treasure.Service;
using Treasure.Service.DBModel;
using Treasure.Service.Utility;

namespace Trasure.Service.Controllers
{
    public class UsersController : ApiController
    {
        private Logger logger = new Logger(typeof(UsersController));      
        [HttpGet]
        public HttpResponseMessage GetUser(int id)
        {
            this.logger.Info("获取用户信息");
            HttpResponseMessage result = null;

            string listjson = string.Empty;
            try
            {
                using (T_Model mode = new T_Model())
                {
                    User user = mode.Users.FirstOrDefault(u => u.id == id);
                    var resultContent = new
                    {
                        id = user.id,
                        name = user.name                       
                    };


                    listjson = Newtonsoft.Json.JsonConvert.SerializeObject(resultContent);
                    result = new HttpResponseMessage(HttpStatusCode.OK);
                    result.Content = new StringContent(listjson, System.Text.Encoding.UTF8, "text/plain");
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("数据库连接出错：" + ex.Message);
                return new HttpResponseMessage(HttpStatusCode.NotFound);

            }

            return result;
        }

        [HttpPost]
        public HttpResponseMessage Login(string username,string password)
        {
            this.logger.Info("请求了菜单树列表");
            HttpResponseMessage result = null;

            string listjson = string.Empty;
            try
            {
                using (T_Model mode = new T_Model())
                {
                    User user = mode.Users.FirstOrDefault(u => u.user.Equals(username) && u.password.Equals(password));
                    if (user != null)
                    {
                        string token = Guid.NewGuid().ToString();
                        user.lastlogin = DateTime.Now;
                        mode.SaveChanges();
                        CacheHelps.InsertSpan<User>(token, user);
                        var resultContent = new
                        {
                             user.id,
                             user.name,
                             user.lastlogin,
                             token
                        };
                        listjson = Newtonsoft.Json.JsonConvert.SerializeObject(resultContent);
                        result = new HttpResponseMessage(HttpStatusCode.OK);
                        result.Content = new StringContent(listjson, System.Text.Encoding.UTF8, "text/plain");
                    }
                    else
                    {
                        result = new HttpResponseMessage((HttpStatusCode)401);
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("数据库连接出错：" + ex.Message);
                return new HttpResponseMessage(HttpStatusCode.NotFound);

            }

            return result;
        }
    }
}
